<?php

require("./mpcamera/setup.php");

$smarty = new Smarty_MPCamera();

$action = strtolower(filter_input(INPUT_GET, "action"));
$submit = strtolower(filter_input(INPUT_POST, "submit"));
$content = strtolower(filter_input(INPUT_GET, "content"));

// OK
if (1 == 1) {
    // Select ThanhPho
    $query_thanhpho = "select * from thanhpho";
    $result_thanhpho = mysql_query($query_thanhpho);
    while ($row = mysql_fetch_array($result_thanhpho)) {
        $thanhpho[] = $row;
    }
    $smarty->assign("thanhpho", $thanhpho);
    // End
    //
    // Select QuanHuyen
    $query_quanhuyen = "
        SELECT `thanhpho`.`MaTP`, `TenTP`, `quanhuyen`.`MaQH`, `TenQH`
        FROM `thanhpho`, `quanhuyen`, `thanhpho_quanhuyen`
        WHERE `thanhpho`.`MaTP`=`thanhpho_quanhuyen`.`MaTP`  AND `quanhuyen`.`MaQH` = `thanhpho_quanhuyen`.`MaQH`
        ;
    ";
    $result_quanhuyen = mysql_query($query_quanhuyen);
    while ($row = mysql_fetch_array($result_quanhuyen)) {
        $quanhuyen[] = $row;
    }
    $smarty->assign("quanhuyen", $quanhuyen);
    // End
    // 
    // Select DiaChi
    $query_diachi = "
        SELECT `MaDC`, CONCAT(`TenTP`, ', ', `TenQH`, ', ', `TenPX`, ', ', `diachi`) AS TenDC
        FROM `diachi`, `phuongxa`, `quanhuyen`,`thanhpho`
        WHERE `diachi`.`MaPX` = `phuongxa`.`MaPX` AND
            `diachi`.`MaQH` = `quanhuyen`.`MaQH` AND
            `diachi`.`MaTP` = `thanhpho`.`MaTP`
        ORDER BY TenDC
        ;
    ";
    $result_diachi = mysql_query($query_diachi);
    while ($row = mysql_fetch_array($result_diachi)) {
        $diachi[] = $row;
    }
    $smarty->assign("diachi_diadiem", $diachi);
    // End
    // Select LienHe
    $query_lienhe = "
        SELECT `MaLHCV`, `chucvu`.`MaCV`, CONCAT(`TenLH`,', ', `SDT`) AS TenLHCV
        FROM `lienhe`, `lienhe_chucvu`, `chucvu`
        WHERE `lienhe_chucvu`.`MaLH` = `lienhe`.`MaLH` AND
              `lienhe_chucvu`.`MaCV` = `chucvu`.`MaCV`
        ;
    ";
    $result_lienhe = mysql_query($query_lienhe);
    while ($row = mysql_fetch_array($result_lienhe)) {
        $lienhe[] = $row;
    }
    $smarty->assign("lienhe", $lienhe);
    // End
    // Gọi hàm này thông qua ajax
    $ajax = filter_input(INPUT_POST, "ajax");
    if (isset($ajax) && $ajax == 2) {
        $data = array(
            "diachi_diadiem" => $diachi,
            "lienhe_diadiem" => $lienhe
        );
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        die();
    }
    // End
}

// OK 2015-02-05
if ($action == "add") {
    // Nothing
}

// OK 2015-02-05
if ($submit == "add") {
    $success = 0;

    $tenDD = filter_input(INPUT_POST, "tenDD");

    $thanhpho = filter_input(INPUT_POST, "thanhpho");
    $quanhuyen = filter_input(INPUT_POST, "quanhuyen");
    $phuongxa = filter_input(INPUT_POST, "phuongxa");
    $address = filter_input(INPUT_POST, "address");

    $tenChuQuan = filter_input(INPUT_POST, "tenChuQuan");
    $sdtChuQuan = filter_input(INPUT_POST, "sdtChuQuan");

    $tenQuanLy = filter_input(INPUT_POST, "tenQuanLy");
    $sdtQuanLy = filter_input(INPUT_POST, "sdtQuanLy");

    // 2015-02-03 
    // Thêm địa chỉ trực tiếp, rồi lấy ra ID, không cần kiểm tra địa chỉ tồn tại, mỗi quán là 1 dòng địa chỉ
    // Khi xóa quán thì xóa luôn dòng địa chỉ
    $query_diachi = "
        INSERT INTO `diachi`(`DiaChi`,`MaPX`,`MaQH`, `MaTP`)
        VALUES(
            '$address',
            $phuongxa,
            $quanhuyen,
            $thanhpho
        );
    ";
    $result_diachi = mysql_query($query_diachi);
    $query_maDC = "
        SELECT MAX(`MaDC`) AS `MaDC`
        FROM `diachi` 
    ";
    $result_maDC = mysql_query($query_maDC);
    $_maDC = mysql_fetch_array($result_maDC);
    $maDC = $_maDC['MaDC'];
    // End
    //
    // 2015-02-05 Thêm chủ quán
    $query_chuquan = "
        INSERT INTO `lienhe`(`TenLH`, `SDT`)
        VALUES('$tenChuQuan','$sdtChuQuan')
        ;
    ";
    $result_chuquan = mysql_query($query_chuquan);
    $query_chuquan_maLH = "
        SELECT MAX(`MaLH`) AS `MaLH`
        FROM `lienhe`
    ";
    $result_chuquan_maLH = mysql_query($query_chuquan_maLH);
    $_chuquan_maLH = mysql_fetch_array($result_chuquan_maLH);
    $chuquan_maLH = $_chuquan_maLH['MaLH'];
    // End
    //
    // 2015-02-05 Thêm quản lý
    $query_quanly = "
        INSERT INTO `lienhe`(`TenLH`, `SDT`)
        VALUES('$tenQuanLy','$sdtQuanLy')
        ;
    ";
    $result_quanly = mysql_query($query_quanly);
    $query_quanly_maLH = "
        SELECT MAX(`MaLH`) AS `MaLH`
        FROM `lienhe`
    ";
    $result_quanly_maLH = mysql_query($query_quanly_maLH);
    $_quanly_maLH = mysql_fetch_array($result_quanly_maLH);
    $quanly_maLH = $_quanly_maLH['MaLH'];
    // End
    //
    // 2015-02-05 Thêm SaleInfo
    $query_saleinfo = "
        INSERT INTO `saleinfo`(`TrafficType`,`QuantityTable`,`QuantityLCD`,`PeopleWeek`,`PeopleDay`,`ViewerDay`)
        VALUES (0,0,0,0,0,0);
    ";
    $result_saleinfo = mysql_query($query_saleinfo);
    $query_maSI = "
        SELECT MAX(`MaSI`) AS `MaSI`
        FROM `saleinfo`
    ";
    $result_maSI = mysql_query($query_maSI);
    $_maSI = mysql_fetch_array($result_maSI);
    $maSI = $_maSI['MaSI'];
    // End
    // 
    // 2015-02-05 Thêm địa điểm
    $query_diadiem = "
        INSERT INTO diadiem(`TenDD`, `MaDC`, `MaChuQuan`, `MaQuanLy`, `MaSI`)
        VALUES(
            '$tenDD',
            $maDC,
            $chuquan_maLH,
            $quanly_maLH,
            $maSI
            );
        ";
    $result = mysql_query($query_diadiem);
    if ($result) {
        $success = 1;
    } else {
        $success = 0;
    }
    $smarty->assign("success", $success);
}

// OK 2015-02-03 ok chỉ view, còn hợp dồng trong đây
if ($action == "view") {
    $query = "
        SELECT `diadiem`.`MaDD`, `diadiem`.`TenDD`, `DiaChi`, `phuongxa`.`TenPX`, `quanhuyen`.`TenQH`, `thanhpho`.`TenTP`, 
                `chuquan`.`SDT` AS `SDT_ChuQuan`, `chuquan`.`TenLH` AS `TenLH_ChuQuan`, 
                `quanly`.`SDT` AS `SDT_QuanLy`, `quanly`.`TenLH` AS `TenLH_QuanLy`, COUNT(`hopdong`.`MaHD`) AS SoHopDong
        FROM (`diadiem`, `diachi`, `phuongxa`, `quanhuyen`, `thanhpho`, `lienhe` `chuquan`, `lienhe` `quanly`)
        LEFT JOIN `hopdong` ON (`hopdong`.`MaDD` = `diadiem`.`MaDD`)
        WHERE `diadiem`.`MaDC` = `diachi`.`MaDC`   
            AND `diachi`.`MaPX` = `phuongxa`.`MaPX` 
            AND `diachi`.`MaQH` = `quanhuyen`.`MaQH`
            AND `diachi`.`MaTP` = `thanhpho`.`MaTP`
            AND `chuquan`.`MaLH` = `diadiem`.`MaChuQuan`
            AND `quanly`.`MaLH` = `diadiem`.`MaQuanLy`
        GROUP BY `diadiem`.`MaDD`, `TenDD`, `DiaChi`, `phuongxa`.`TenPX`, `quanhuyen`.`TenQH`, `thanhpho`.`TenTP`
        ORDER BY `thanhpho`.`TenTP`, `quanhuyen`.`TenQH`, `phuongxa`.`TenPX`, `DiaChi`, `TenDD` 
    ;
    ";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $address[] = $row;
    }
    $smarty->assign("address", $address);

    if ($content == "hopdong") {
        $id = (int) $smarty->clean(filter_input(INPUT_GET, "id"), true);

        $str_id = "";
        if ($id > 0) {
            $str_id = "AND `hopdong`.`MaDD` = $id";
        }
        $query = "
            SELECT `ID`, `diadiem`.`MaDD`, `diadiem`.`TenDD`, `MaHD`, `SoThang`,`Gia`, `SoLuong`, `SoNgayBaoDuong`, 
                `ThoiGianBatDau`, (`ThoiGianBatDau` + INTERVAL `SoThang` MONTH + INTERVAL `SoNgayBaoDuong` DAY) AS `ThoiGianKetThuc`, 
                `TenPTTT`, FLOOR(`SoThang` / `hopdong`.`MaPTTT`) AS SoDotThanhToan, `dotthanhtoan`.`MaDTT`, `TenDTT`, `HieuLuc`, `GhiChu`,
                `ChuTK`, `SoTK`, `TenNH`
            FROM `diadiem`, `hopdong`, `phuongthucthanhtoan`, `dotthanhtoan`
            WHERE `hopdong`.`MaDD` = `diadiem`.`MaDD`
                AND `hopdong`.`MaPTTT` = `phuongthucthanhtoan`.`MaPTTT`
                AND  (DAY(`ThoiGianBatDau`) BETWEEN `dotthanhtoan`.`NgayBatDau` AND `dotthanhtoan`.`NgayKetThuc`)
                $str_id
                ;
            ";
        $result = mysql_query($query);
        while ($row = mysql_fetch_array($result)) {
            $diadiem_hopdong[] = $row;
        }
        $smarty->assign("diadiem_hopdong", $diadiem_hopdong);
        $smarty->assign("id", $id);
        $smarty->display("diadiem_hopdong.tpl");
        die();
    }
}

// OK 2015-02-03
if ($action == "edit") {
    $found = 0;
    $_id = filter_input(INPUT_GET, "id");
    $id = $smarty->clean($_id, true);
    if ($id > 0) {
        $query = "
            SELECT  `MaDD`, `TenDD`, `DiaChi`,`MaPX`,`MaQH`,`MaTP`, 
                    `chuquan`.`TenLH` AS `TenLH_ChuQuan`, `chuquan`.`SDT` AS `SDT_ChuQuan`,
                    `quanly`.`TenLH` AS `TenLH_QuanLy`, `quanly`.`SDT` AS `SDT_QuanLy`
            FROM `diadiem`,`diachi`,`lienhe` AS `chuquan`, `lienhe` AS `quanly`
            WHERE
                    `diadiem`.`MaDC` = `diachi`.`MaDC` AND
                    `diadiem`.`MaChuQuan` = `chuquan`.`MaLH` AND
                    `diadiem`.`MaQuanLy` = `quanly`.`MaLH` AND
                    `MaDD` = $id
            ;
        ";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 1) {
            $found = 1;
            while ($row = mysql_fetch_array($result)) {
                $diadiem[] = $row;
            }
            $smarty->assign("diadiem", $diadiem);
        } else {
            header("Location: /diadiem/view");
        }
    } else {
        //$action = null;
        header("Location: /diadiem/view");
    }
}

// OK 2015-02-05
if ($submit == "update") {
    $success = 0;
    $maDD = filter_input(INPUT_POST, "maDD");

    $tenDD = filter_input(INPUT_POST, "tenDD");

    $thanhpho = filter_input(INPUT_POST, "thanhpho");
    $quanhuyen = filter_input(INPUT_POST, "quanhuyen");
    $phuongxa = filter_input(INPUT_POST, "phuongxa");
    $address = filter_input(INPUT_POST, "address");

    $tenChuQuan = filter_input(INPUT_POST, "tenChuQuan");
    $sdtChuQuan = filter_input(INPUT_POST, "sdtChuQuan");

    $tenQuanLy = filter_input(INPUT_POST, "tenQuanLy");
    $sdtQuanLy = filter_input(INPUT_POST, "sdtQuanLy");


    $query_diadiem = "
        SELECT *
        FROM `diadiem`
        WHERE `MaDD` = $maDD
        ;
    ";
    $result_diadiem = mysql_query($query_diadiem);

    if (mysql_num_rows($result_diadiem) != 1) {
        $success = -1;
    } else {
        $diadiem = mysql_fetch_array($result_diadiem);
        $maDC = $diadiem['MaDC'];
        $maLH_CQ = $diadiem['MaChuQuan'];
        $maLH_QL = $diadiem['MaQuanLy'];
        
        // Update diadiem
        $query_update_diadiem = "
            UPDATE `diadiem`
            SET `TenDD`= '$tenDD'
            WHERE `MaDD` = $maDD
            ;
        ";
        $result_update_diadiem = mysql_query($query_update_diadiem);
        
        // Update diachi
        $query_update_diachi = "
            UPDATE `diachi`
            SET `DiaChi` = '$address',
                `MaPX` = $phuongxa,
                `MaQH` = $quanhuyen,
                `MaTP` = $thanhpho
            WHERE `MaDC` = $maDC
            ;
        ";
        $result_update_diachi = mysql_query($query_update_diachi);
        
        // Update chuquan
        $query_update_chuquan = "
            UPDATE `lienhe`
            SET `TenLH` = '$tenChuQuan',
                `SDT` = '$sdtChuQuan'
            WHERE `MaLH` = $maLH_CQ
            ;
        ";
        $result_update_chuquan = mysql_query($query_update_chuquan);
        
        // Update quanly
        $query_update_quanly = "
            UPDATE `lienhe`
            SET `TenLH` = '$tenQuanLy',
                `SDT` = '$sdtQuanLy'
            WHERE `MaLH` = $maLH_QL
            ;
        ";
        $result_update_quanly = mysql_query($query_update_quanly);
        
        if ($result_update_diadiem && $result_update_diachi && $result_update_chuquan && $result_update_quanly) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);
}

// OK
if ($action == "delete") {
    $success = 0;

    $maDD = filter_input(INPUT_GET, "id");

    $query = "
        SELECT *
        FROM `diadiem`
        WHERE `MaDD`=$maDD
        ;
    ";
    $result = mysql_query($query);

    if (mysql_num_rows($result) == 0) {
        $success = -1;
    } else {
        $query = "
            DELETE FROM `diadiem`
            WHERE `MaDD` = $maDD
            ;
        ";
        $result = mysql_query($query);
        if ($result) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);
}

$smarty->assign("action", $action);

$smarty->display("diadiem.tpl");
